package sharping.appilog.fragment;

import com.handmark.pulltorefresh.library.PullToRefreshListView;

import sharping.appilog.R;
import sharping.appilog.Logic.ScreenSize;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.TextView;

public class BaseTabListFragment extends Fragment {

	private FrameLayout notList;
	private ProgressBar progressBar;
	private LinearLayout emptyView;
	private PullToRefreshListView list;
	private RadioButton tab1, tab2;
	
	private OnTabSelected mOnTabSelected; 
	
	public interface OnTabSelected {
		void onTabSelected(int position);
	}
	
	public void setOnTabSelected(OnTabSelected onTabSelected) {
		this.mOnTabSelected = onTabSelected;
	}
	
	public RadioButton getTab1() {
		return tab1;
	}
	
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setListShown(false);
		setLayout();
	}
	
	private void setLayout() {
		list.setPullToRefreshEnabled(false);
		list.setVerticalScrollBarEnabled(false);
		list.getRefreshableView().setDivider(null);
		list.getRefreshableView().setDividerHeight(ScreenSize.DPtoPX(getActivity(), 4));
		// list.setDividerHeight(ScreenSize.DPtoPX(getActivity(), 4));
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View rootView = inflater.inflate(R.layout.tablist, null);
		notList = (FrameLayout) rootView.findViewById(R.id.list_load);
		progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar1);
		list = (PullToRefreshListView) rootView.findViewById(R.id.tablist_list);
		tab1 = (RadioButton) rootView.findViewById(R.id.radioButton1);
		tab2 = (RadioButton) rootView.findViewById(R.id.radioButton2);
		emptyView = (LinearLayout) rootView.findViewById(R.id.empty);
		
		tab1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					tab1.setTextColor(getActivity().getResources().getColor(R.color.MainColor));
					tab2.setTextColor(getActivity().getResources().getColor(R.color.text_timeline_title));
					mOnTabSelected.onTabSelected(0);
				} else {
					tab1.setTextColor(getActivity().getResources().getColor(R.color.text_timeline_title));
					tab2.setTextColor(getActivity().getResources().getColor(R.color.MainColor));
					mOnTabSelected.onTabSelected(1);
				}
			}
		});
		
		return rootView;
	}
	
	public void setListAdapter(ListAdapter adapter) {
		list.setAdapter(adapter);
		setListShown(true);
	}
	
	public void setListEmpty() {
		setListShown(false);
		progressBar.setVisibility(View.GONE);
		emptyView.setVisibility(View.VISIBLE);
	}
	
	public void setListShown(boolean state) {
		if (state) {
			notList.setVisibility(View.GONE);
			list.setVisibility(View.VISIBLE);
		} else {
			notList.setVisibility(View.VISIBLE);
			emptyView.setVisibility(View.GONE);
			list.setVisibility(View.GONE);
		}
	}
	
	public PullToRefreshListView getListView() {
		return list;
	}
}
